#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;

namespace Drill.Core.Utilities
{
	/// <summary>
	/// Provides argument validation utility methods.
	/// </summary>
    public static class ValidationUtility
    {
		/// <summary>
		/// Validates that the value is not an empty string.
		/// </summary>
		/// <param name="value">The value to be validated.</param>
		/// <param name="paramName">Name of the parameter.</param>
		/// <returns>
		/// A null value if the value has been validated successfully, otherwise an instance of 
		/// <see cref="ArgumentException"/> that may be thrown by the caller to preserve the call stack integrity.
		/// </returns>
		/// <exception cref="System.ArgumentException">The paramName parameter cannot be null, empty or contain only whitespace characters.</exception>
        public static ArgumentException GetArgumentExceptionForStringIsEmpty(string value, string paramName)
        {
            if (string.IsNullOrEmpty(paramName) || string.IsNullOrWhiteSpace(paramName))
            {
                throw new ArgumentException("The paramName parameter cannot be null, empty or contain only whitespace characters.", "paramName");
            }

            if (null == value)
            {
                // Cannot test a null value.
                return null;
            }

            // Check for empty string value.
            if (string.IsNullOrEmpty(value))
            {
                return new ArgumentException(
                    string.Format("The {0} parameter is not in the correct format. The string cannot be empty.", paramName)
                  , paramName
                  );
            }

            // String value passes test.
            return null;
        }

		/// <summary>
		/// Validates that the value is not a null string.
		/// </summary>
		/// <param name="value">The value to be validated.</param>
		/// <param name="paramName">Name of the parameter.</param>
		/// <returns>
		/// A null value if the value has been validated successfully, otherwise an instance of 
		/// <see cref="ArgumentException"/> that may be thrown by the caller to preserve the call stack integrity.
		/// </returns>
		/// <exception cref="System.ArgumentException">The paramName parameter cannot be null, empty or contain only whitespace characters.</exception>
        public static ArgumentException GetArgumentExceptionForStringIsNull(string value, string paramName)
        {
            if (string.IsNullOrEmpty(paramName) || string.IsNullOrWhiteSpace(paramName))
            {
                throw new ArgumentException("The paramName parameter cannot be null, empty or contain only whitespace characters.", "paramName");
            }

            // Check for null value.
            if (null == value)
            {
                return new ArgumentNullException(
                    paramName
                  , string.Format("The {0} parameter cannot be null.", paramName)
                    );
            }

            // String value passes test.
            return null;
        }

		/// <summary>
		/// Validates that the value is not a string containing only whitespace characters.
		/// </summary>
		/// <param name="value">The value to be validated.</param>
		/// <param name="paramName">Name of the parameter.</param>
		/// <returns>
		/// A null value if the value has been validated successfully, otherwise an instance of 
		/// <see cref="ArgumentException"/> that may be thrown by the caller to preserve the call stack integrity.
		/// </returns>
		/// <exception cref="System.ArgumentException">The paramName parameter cannot be null, empty or contain only whitespace characters.</exception>
        public static ArgumentException GetArgumentExceptionForStringContainsOnlyWhitespace(string value, string paramName)
        {
            if (string.IsNullOrEmpty(paramName) || string.IsNullOrWhiteSpace(paramName))
            {
                throw new ArgumentException("The paramName parameter cannot be null, empty or contain only whitespace characters.", "paramName");
            }

            if (string.IsNullOrEmpty(value))
            {
                // Cannot test a null or empty value. (Ok, technically we can test an empty value.)
                return null;
            }

            if (string.IsNullOrWhiteSpace(value))
            {
                return new ArgumentException(
                    string.Format("The {0} parameter is not in the correct format. The string cannot contain only whitespace characters.", paramName)
                  , paramName
                  );
            }

            // String value passes test.
            return null;
        }

		/// <summary>
		/// Validates that the value is not a null or empty string or a string containing only whitespace characters.
		/// </summary>
		/// <param name="value">The value to be validated.</param>
		/// <param name="paramName">Name of the parameter.</param>
		/// <returns>
		/// A null value if the value has been validated successfully, otherwise an instance of 
		/// <see cref="ArgumentException"/> that may be thrown by the caller to preserve the call stack integrity.
		/// </returns>
		/// <exception cref="System.ArgumentException">The paramName parameter cannot be null, empty or contain only whitespace characters.</exception>
        public static ArgumentException GetArgumentExceptionForStringIsNullOrIsEmptyOrContainsOnlyWhitespace(string value, string paramName)
        {
            if (string.IsNullOrEmpty(paramName) || string.IsNullOrWhiteSpace(paramName))
            {
                throw new ArgumentException("The paramName parameter cannot be null, empty or contain only whitespace characters.", "paramName");
            }

            // Check for null value.
            ArgumentException argumentException = GetArgumentExceptionForStringIsNull(value, paramName);
            if (null != argumentException)
            {
                return argumentException;
            }

            // Check for empty string value.
            argumentException = GetArgumentExceptionForStringIsEmpty(value, paramName);
            if (null != argumentException)
            {
                return argumentException;
            }

            // Check for string contains only whitespace characters.
            argumentException = GetArgumentExceptionForStringContainsOnlyWhitespace(value, paramName);
            if (null != argumentException)
            {
                return argumentException;
            }

            // String value passes all tests.
            return null;
        }
    }
}